import request from '@/utils/request'
import type { ApiResponse, PageResult } from '@/types/api'

export interface AddressDTO {
  id?: number
  userId?: number
  province: string
  city: string
  town: string
  mobile: string
  street: string
  contact: string
  isDefault: number
  notes?: string
}

// 分页查询参数
export interface AddressQuery {
  pageNum: number
  pageSize: number
  userId?: number
  province?: string
  city?: string
  town?: string
  street?: string
  contact?: string
  mobile?: string
  isDefault?: number
}

// 获取地址列表（分页）
export const getAddressList = (params: AddressQuery) => {
  return request.get<ApiResponse<PageResult<AddressDTO>>>('/addresses/page', { params })
}

// 获取地址详情
export const getAddressById = (id: number) => {
  return request.get<ApiResponse<AddressDTO>>(`/addresses/${id}`)
}

// 新增地址
export const createAddress = (data: Omit<AddressDTO, 'id'>) => {
  return request.post<ApiResponse<void>>('/addresses', data)
}

// 更新地址
export const updateAddress = (id: number, data: Partial<AddressDTO>) => {
  return request.put<ApiResponse<void>>(`/addresses/${id}`, data)
}

// 删除地址
export const deleteAddress = (id: number) => {
  return request.delete<ApiResponse<void>>(`/addresses/${id}`)
}

// 设置默认地址
export const setDefaultAddress = (id: number) => {
  return request.put<ApiResponse<void>>(`/addresses/${id}/default`)
}

// 获取当前用户的地址列表
export const getMyAddresses = () => {
  return request.get<ApiResponse<AddressDTO[]>>('/addresses')
}

 